﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 职工端
{
    public partial class 添加新商品 : Form
    {
        string constr=denglu.strcon;
        string gid;
        string sid;

        //商品id靠随机数生成
        //供货商id靠随机数生成
        public 添加新商品()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tbx_gname.Text == "" || tbx_gprice.Text == "" || tbx_gtype.Text == "" || tbx_num.Text == "" || tbx_sname.Text == "" || tbx_sprice.Text == "" || tbx_sname.Text == "" || tbx_phone.Text == "" || tbx_address.Text == "")
            {
                MessageBox.Show("信息未填完整，请继续输入！", "提示");
                return;
            }
            else
            {
                Random ra = new Random();
                gid = ra.Next().ToString();
                Random ran = new Random();
                sid = ran.Next().ToString();
                using (SqlConnection sqlcon = new SqlConnection(constr))
                {
                    try
                    {
                        sqlcon.Open();
                        string sql = "insert into Goods values('" + gid + "','" + tbx_gname.Text + "','" + tbx_gtype.Text + "','" + tbx_gprice.Text + "','" + tbx_sprice.Text + "'," + int.Parse(tbx_num.Text) + ",'" + sid + "')";
                        SqlCommand cmd = new SqlCommand(sql, sqlcon);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("添加成功！", "提示");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString() + "添加失败！");
                    }
                    finally
                    {
                        sqlcon.Close();
                    }
                }
                using (SqlConnection sqlcon = new SqlConnection(constr))
                {
                    try
                    {
                        sqlcon.Open();
                        string sql = "insert into Supplier values('" + sid + "','" + gid + "','" + tbx_sname.Text + "','" + tbx_phone.Text + "','" + tbx_address.Text + "')";
                        SqlCommand cmd = new SqlCommand(sql, sqlcon);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("添加成功！", "提示");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString() + "添加失败！", "提示");
                    }
                    finally
                    {
                        sqlcon.Close();
                    }
                }
            }
        }
    }
}
